package io.renren.modules.generator.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

import io.renren.modules.generator.entity.BsnGoutongzhidaoRelationEntity;
import io.renren.modules.generator.service.BsnGoutongzhidaoRelationService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 沟通指导关系表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-07-08 10:09:49
 */
@RestController
@RequestMapping("generator/bsngoutongzhidaorelation")
public class BsnGoutongzhidaoRelationController {
    @Autowired
    private BsnGoutongzhidaoRelationService bsnGoutongzhidaoRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:bsngoutongzhidaorelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bsnGoutongzhidaoRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{zhidaoid}")
    @RequiresPermissions("generator:bsngoutongzhidaorelation:info")
    public R info(@PathVariable("zhidaoid") String zhidaoid){
			BsnGoutongzhidaoRelationEntity bsnGoutongzhidaoRelation = bsnGoutongzhidaoRelationService.selectById(zhidaoid);

        return R.ok().put("bsnGoutongzhidaoRelation", bsnGoutongzhidaoRelation);
    }

    /**
     * 保存
     */
    @SuppressWarnings("unchecked")
	@RequestMapping("/save")
    public R save(@RequestBody Map<String, Object> params){
    	
    	BsnGoutongzhidaoRelationEntity relation = new BsnGoutongzhidaoRelationEntity();
    	relation.setZhidaoid(params.get("zhidaoId").toString());
    	relation.setMentorid(params.get("mentorId").toString());
    	
    	ArrayList<String> students = (ArrayList<String>)params.get("students");
    	for (String studentId : students) {
			relation.setStundentid(studentId);
			int ret = bsnGoutongzhidaoRelationService.selectCount(
					new EntityWrapper<BsnGoutongzhidaoRelationEntity>(relation));
			if (ret > 0) {
				continue;
			}
			BsnGoutongzhidaoRelationEntity insertEntity = new BsnGoutongzhidaoRelationEntity();
			insertEntity.setZhidaoid(relation.getZhidaoid());
			insertEntity.setMentorid(relation.getMentorid());
			insertEntity.setStundentid(studentId);
			insertEntity.setCreateTime(new Date());
			bsnGoutongzhidaoRelationService.insert(insertEntity);
		}

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:bsngoutongzhidaorelation:update")
    public R update(@RequestBody BsnGoutongzhidaoRelationEntity bsnGoutongzhidaoRelation){
			bsnGoutongzhidaoRelationService.updateById(bsnGoutongzhidaoRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:bsngoutongzhidaorelation:delete")
    public R delete(@RequestBody String[] zhidaoids){
			bsnGoutongzhidaoRelationService.deleteBatchIds(Arrays.asList(zhidaoids));

        return R.ok();
    }

}
